package com.zlj.xialiao.interceptor;

/**
 * @author 郑霖俊
 * @create 2021-05-06 9:59
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.zhneg
 * @create 2019-11-06-21:05
 * 定义登录拦截器
 * 所有访问都要经过这个拦截器
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    Logger logger = LoggerFactory.getLogger(getClass());

    //调用目标方法之前被拦截，比如maint/s/a.html进来，要拦截
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws ServletException, IOException {

        Object loginuser= request.getSession().getAttribute("loginUser");
        if(loginuser!=null){
            logger.info("有权限,登录。。。" );

            //已经登陆过放行
            return true;
        }

        logger.info("session"+(String) loginuser);
        //没有登陆过
        logger.info("没有权限请先登录。。。" );

        request.setAttribute("msg","没有权限请先登录，返回到登录界面");
        //返回到管理员的登录界面
        request.getRequestDispatcher("/login").forward(request,response);
        return false;

    }
}

